package my.maven.project.spiderproc.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import my.maven.project.spiderproc.dao.WordDao;
import my.maven.project.spiderproc.entity.Word;
import my.maven.project.spiderproc.service.IWordService;
import my.maven.project.spiderproc.spiders.CibaSpider;


@Service
public class WordServiceImpl implements IWordService {

	@Autowired
	private WordDao wordDao;
	
	public List<Word> getList()
	{
		return wordDao.findAll();
	}
	
	public Word findByWords(String words)
	{
		return wordDao.findByWords(words);
	}
	
	public Word getOne(long Id)
	{
		return wordDao.getOne(Id);
	}
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void add(Word entity)
	{
	   wordDao.save(entity);
	}
	
	@Transactional
	public void addList(List<Word> list)
	{		
		List<String> addword = list.stream().map(e-> e.getWords()).collect(Collectors.toList());
		List<String> words = wordDao.listByWords(addword);
		for (Word entity : list) {
			if(!words.contains(entity.getWords()))
				wordDao.save(entity);
		}
	}
	
	@Transactional
	public void addList2(List<Word> list)
	{		 
		for (Word entity : list) {
			 wordDao.save(entity);
		}
	}
	
	/**
	 * NESTED 嵌套事务 MANDATORY 当前事务不存在，则会抛出一个异常 REQUIRED_NEW 一个新的事务将被启动。如果存在当前事务，在该方法执行期间，当前事务会被挂起。
	 */
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void SpiderList(Collection<String> words) throws IOException {
		List<String> existswords = wordDao.listByWords(new ArrayList<String>(words));
		List<Word> WordList = new ArrayList<>();
		for (String word : words) {
			if (!existswords.contains(word)) {
				Map<String, String> data = CibaSpider.ReuqestHttp(word);
				if (data != null) {
					Word entity = new Word();
					entity.setCreateDate(new Date());
					entity.setMeans(data.get("means"));
					entity.setPronunce(data.get("pronunce"));
					entity.setWordChange(data.get("changes"));
					entity.setWords(data.get("words"));
					WordList.add(entity);
				}
			}
		}
		addList2(WordList);
	}
	
	@Transactional
	public void delete(long Id)
	{
		wordDao.delete(Id);
	}
	
	
	@Transactional
	public boolean update(Word entity)
	{
		Word data = wordDao.getOne(entity.getId());
		if(data != null)
		{
			data.setWords(entity.getWords()); 
			data.setMeans(entity.getMeans());
			data.setPronunce(entity.getPronunce());
			data.setWordChange(entity.getWordChange());			
			wordDao.save(data);
			return true;
		} 
		return false;
	}
	
	public boolean existsByWords(String words)
	{
		return wordDao.existsByWords(words);
	}
			
	public List<Word> findByWordsLike(String words)
	{
		return wordDao.findByWordsLike(words);
	}
	
	public List<Word> findByWordsIn(Collection<String> Words){
		return wordDao.findByWordsIn(Words);
	}
	
}
